<?php
/**
 * communit.as
 * @copyright (C)2008 J. William McCarthy, All Rights Reserved.
 * @category communitas
 * @package communitas
 * @author J. William McCarthy
 * @link http://communit.as communit.as
 * @license http://communit.as/docs/license License
 */

/**
 * Smarty {cms_page} function plugin
 *
 * Type:     function<br>
 * Name:     cms<br>
 * Date:     May 31, 2007<br>
 * Purpose:	 Pulls content from cms  
 * 
 * Input:
 *         	  	- slug = article slug in wordpress (string)
 * 				- paragraphs = wrap each line in paragraph tags (boolean)
 *
 * @author Jaybill McCarthy <heybill@jaybill.com>
 * @version  0.2
 * @param array
 * @param Smarty
 * @return string|null
 */
function smarty_function_cms($params, &$smarty){

	$content = null;
	$page_table = new Cms_Pages();
	$page = $page_table->getBySlug($params['slug']);
	if(array_key_exists('part',$params)){
		$part = $params['part'];
	}

	if(!is_null($page)){
		if(isset($part)){
			switch ($part){
				case "title":
					$content = $page->title;					
				break;
				case "author":
					$content = $page->author_display_name;
				break;
				case "posted_on":
					$content = strtotime($page->posted_on);
				break;
			}
		} else {			
			$content = $page->text;
		}

	} else {
		if(isset($part)){
			switch ($part){
				case "title":
					$content = "Lorem Ipsum Dolor Sit Amet";					
				break;
				case "author":
					$content = "Imelda Donkavich";
				break;
				case "posted_on":
					$content = mktime();
				break;
			}
		} else {			
			$chars = null;
			if(array_key_exists('chars',$params)){
				$chars = $params['chars'];
			}		
			$content = App_Common::makeDummyText($chars);
		}		
	}

	if(array_key_exists('paragraphs',$params) and is_null($part)){
		if($params['paragraphs'] == "true"){
			$content = App_Common::makeParagraphs($content);
		}
	}

	if(is_null($page)){
		$content = "\n<!-- placeholder content for slug '".$params['slug']."' -->\n" . $content;
	}

	return $content;
}
